home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Pentominoes 2.0 / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-26  |  15.2 KB  |  509 lines  |  [TEXT/MMCC]

  1. #include "menus.h"
  2. #include "program globals.h"
  3. #include "key layer.h"
  4. #include "help.h"
  5. #include "environment.h"
  6. #if USE_SOUNDS
  7. #include "sound layer.h"
  8. #endif
  9. #if USE_MUSIC
  10. #include "music layer.h"
  11. #endif
  12. #if USE_PRINTING
  13. #include "printing layer.h"
  14. #endif
  15. #include "program init.h"
  16. #include "resource layer.h"
  17. #include "graphics dispatch.h"
  18. #include "memory layer.h"
  19. #include "window layer.h"
  20. #if USE_WINDOWS_MENU
  21. #include "windows menu.h"
  22. #endif
  23. #if USE_SAVE
  24. #include "save dialog.h"
  25. #include "pent load-save.h"
  26. #endif
  27. #include "pent.h"
  28. #include "pent main window.h"
  29. #include "pent editor window.h"
  30. #include "pent editor files.h"
  31. #include "pent editor lists.h"
  32. #include "pent editor dispatch.h"
  33. #include "pent editor dialogs.h"
  34. #include "pent meat.h"
  35. #include "pent floating window.h"
  36. #include "pent globals.h"
  37.  
  38. static    MenuHandle        gAppleMenu=0L;
  39. static    MenuHandle        gFileMenu=0L;
  40. static    MenuHandle        gEditMenu=0L;
  41. static    MenuHandle        gBoardMenu=0L;
  42. static    MenuHandle        gOptionsMenu=0L;
  43. static    MenuHandle        gEditorMenu=0L;
  44. #if USE_WINDOWS_MENU
  45.         MenuHandle        gWindowsMenu=0L;
  46. #endif
  47.  
  48. enum
  49. {
  50.     mApple = 400, mFile, mEdit, mBoard, mOptions, mEditor,
  51. #if USE_WINDOWS_MENU
  52.         mWindows,
  53. #endif
  54.     
  55.     iAbout = 1, iOtherProducts, iHelp,
  56.     
  57.     iNew = 1, iOpen, file_unused1, iClose, iSave, iSaveAs, file_unused2, iPageSetup, iPrint,
  58.         file_unused3, iQuit,
  59.     
  60.     iUndo = 1, edit_unused1, iCut, iCopy, iPaste, iClear, edit_unused2, iSelectAll,
  61.     
  62.     iPlace = 1, iRemove, options_unused1, iSound, iMusic, iMessages, iEffects, iAskBeforeClosing,
  63.         options_unused2, iShowFloat, iAnchorFloat, options_unused3, iAutoSize, iPieceSize,
  64.     
  65.     iOpenEditor = 1, editor_unused1, iNewGroup, iNewBoard, iEditGroup, iDeleteGroup,
  66.         editor_unused2, iClearBoard, iLockBoard, iSaveBoard, editor_unused3, iMessageBox,
  67.         iToolbar,
  68.     
  69. #if USE_WINDOWS_MENU
  70.     iZoom = 1, iSendToBack
  71. #else
  72.     enumDummy
  73. #endif
  74. };
  75.  
  76. /*-----------------------------------------------------------------------------------*/
  77. /* internal stuff for menus.c                                                        */
  78.  
  79. static    void HandleAppleMenu(short menuItem);
  80. static    void HandleFileMenu(short menuItem);
  81. static    void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough);
  82. static    void HandleOptionsMenu(short menuItem);
  83. static    void HandleEditorMenu(short menuItem);
  84. #if USE_WINDOWS_MENU
  85. static    void HandleWindowsMenu(short menuItem);
  86. #endif
  87. static    void HandleSubMenu(short menuID, short menuItem);
  88. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition);
  89.  
  90. Boolean InitTheMenus(void)
  91. {
  92.     Handle            MBARHandle;
  93.     
  94.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  95.         return FALSE;
  96.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  97.     
  98.     if ((gAppleMenu=GetMHandle(mApple))==0L)    /* GetNewMBar also got menu handles of */
  99.         return FALSE;
  100.     if ((gFileMenu=GetMHandle(mFile))==0L)    /* every menu it includes, so just */
  101.         return FALSE;
  102.     if ((gEditMenu=GetMHandle(mEdit))==0L)    /* grab these handles and assign them */
  103.         return FALSE;
  104.     if ((gBoardMenu=GetMHandle(mBoard))==0L)
  105.         return FALSE;
  106.     if ((gOptionsMenu=GetMHandle(mOptions))==0L)
  107.         return FALSE;
  108.     if ((gEditorMenu=GetMHandle(mEditor))==0L)
  109.         return FALSE;
  110.     
  111. #if USE_WINDOWS_MENU
  112.     if ((gWindowsMenu=GetMHandle(mWindows))==0L)
  113.         return FALSE;
  114. #endif
  115.     
  116.     InitBoardsFileFS();
  117.     RebuildBoardMenu();
  118.     
  119.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  120.     
  121.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  122.     DrawMenuBar();                                /* draws the actual menu bar */
  123.     
  124.     return TRUE;
  125. }
  126.  
  127. void ShutDownTheMenus(void)
  128. {
  129.     gAppleMenu=SafeDisposeMenu(gAppleMenu);
  130.     gFileMenu=SafeDisposeMenu(gFileMenu);
  131.     gEditMenu=SafeDisposeMenu(gEditMenu);
  132.     gBoardMenu=SafeDisposeMenu(gBoardMenu);
  133.     gOptionsMenu=SafeDisposeMenu(gOptionsMenu);
  134.     gEditorMenu=SafeDisposeMenu(gEditorMenu);
  135. }
  136.  
  137. void AdjustMenus(void)
  138. {
  139.     short            kind, i;
  140.     WindowRef        front, frontDoc, mainWindow, editorWindow;
  141.     Boolean            editorIsFront, groupListActive, boardListActive, groupHighlighted;
  142.     Boolean            boardHighlighted, boardLocked;
  143. #if USE_WINDOWS_MENU
  144.     short            numOpenWindows;
  145. #endif
  146.     
  147.     front=FrontWindow();
  148.     kind=front ? ((WindowPeek)front)->windowKind : 0;
  149.     frontDoc=GetFrontDocumentWindow();
  150.     mainWindow=GetIndWindowRef(kMainWindow);
  151.     editorWindow=GetIndWindowRef(kEditorWindow);
  152.     editorIsFront=(frontDoc!=0L) && (frontDoc==editorWindow);
  153.     groupListActive=(editorWindow!=0L) && GroupListActiveQQ();
  154.     boardListActive=(editorWindow!=0L) && BoardListActiveQQ();
  155.     groupHighlighted=(editorWindow!=0L) && (GetHighlightedGroup()>=0);
  156.     boardHighlighted=(editorWindow!=0L) && (GetHighlightedBoard()>=0);
  157.     boardLocked=(editorWindow!=0L) && HighlightedBoardLockedQQ();
  158. #if USE_WINDOWS_MENU
  159.     numOpenWindows=GetNumberOfOpenWindows();
  160. #endif
  161.     
  162.     EDItem(gAppleMenu, 0, TRUE);
  163.     EDItem(gAppleMenu, iAbout, TRUE);
  164.     EDItem(gAppleMenu, iOtherProducts, TRUE);
  165.     EDItem(gAppleMenu, iHelp, TRUE);
  166.     
  167.     EDItem(gFileMenu, 0, TRUE);
  168.     EDItem(gFileMenu, iNew, (gCurrentBoardIndex>-1) && ((mainWindow==0L) ||
  169.         (!WindowIsModifiedQQ(mainWindow)) || (!gAskBeforeClosing)));
  170.     EDItem(gFileMenu, iClose, front!=0L);
  171. #if USE_SAVE
  172.     EDItem(gFileMenu, iOpen, (mainWindow==0L) ||
  173.         (!WindowIsModifiedQQ(mainWindow)) || (!gAskBeforeClosing));
  174.     EDItem(gFileMenu, iSave, (mainWindow!=0L) && (WindowHasLayer(mainWindow)) &&
  175.         (WindowIsModifiedQQ(mainWindow)));
  176.     EDItem(gFileMenu, iSaveAs, mainWindow!=0L);
  177. #endif
  178. #if USE_PRINTING
  179.     EDItem(gFileMenu, iPageSetup, TRUE);
  180.     EDItem(gFileMenu, iPrint, (frontDoc!=0L) && (WindowHasLayer(frontDoc)) &&
  181.         (WindowIsPrintableQQ(frontDoc)));
  182. #endif
  183.     EDItem(gFileMenu, iQuit, TRUE);
  184.     
  185.     EDItem(gEditMenu, 0, TRUE);
  186.     EDItem(gEditMenu, iUndo, (kind<0) || ((mainWindow!=0L) && (frontDoc==mainWindow) && ((gNumPlayed>0) || (gNumHilited>0))));
  187.     EDItem(gEditMenu, iCut, kind<0);
  188.     EDItem(gEditMenu, iCopy, kind<0);
  189.     EDItem(gEditMenu, iPaste, kind<0);
  190.     EDItem(gEditMenu, iClear, kind<0);
  191.     EDItem(gEditMenu, iSelectAll, kind<0);
  192.     
  193.     EDItem(gBoardMenu, 0, TRUE);
  194.     
  195.     EDItem(gOptionsMenu, 0, TRUE);
  196.     EDItem(gOptionsMenu, iPlace, (mainWindow!=0L) && (frontDoc==mainWindow) && (gNumHilited==5) && (gSelectedPieceColor==kSelectedTile));
  197.     EDItem(gOptionsMenu, iRemove, (mainWindow!=0L) && (frontDoc==mainWindow) && (gSelectedPieceColor!=kSelectedTile));
  198.     EDItem(gOptionsMenu, iSound, gSoundAvailable);
  199.     EDItem(gOptionsMenu, iMusic, gMusicAvailable);
  200.     CheckItem(gOptionsMenu, iSound, gSoundToggle&&gSoundAvailable);
  201.     CheckItem(gOptionsMenu, iMusic, gMusicToggle&&gMusicAvailable);
  202.     CheckItem(gOptionsMenu, iMessages, gDisplayMessages);
  203.     CheckItem(gOptionsMenu, iEffects, gGraphicEffects);
  204.     CheckItem(gOptionsMenu, iAskBeforeClosing, gAskBeforeClosing);
  205.     CheckItem(gOptionsMenu, iShowFloat, gShowFloat);
  206.     CheckItem(gOptionsMenu, iAnchorFloat, gAnchorFloat);
  207.     CheckItem(gOptionsMenu, iAutoSize, gAutoSize);
  208.     
  209.     for (i=1; i<=CountMItems(gBoardMenu); i++)
  210.         EDItem(gBoardMenu, i, mainWindow==0L);
  211.     SetBoardCheckmark();
  212.     
  213.     SetItem(gEditorMenu, iOpenEditor, (editorWindow==0L) ? "\pOpen board editor" : "\pBring board editor to front");
  214.     SetItem(gEditorMenu, iEditGroup, groupListActive ? "\pEdit group name" :
  215.         "\pEdit board name and dimensions");
  216.     SetItem(gEditorMenu, iDeleteGroup, groupListActive ? "\pDelete group" : "\pDelete board");
  217.     SetItem(gEditorMenu, iLockBoard, boardLocked ? "\pUnlock board" : "\pLock board");
  218.     SetItem(gEditorMenu, iMessageBox, gShowMessageBox ? "\pHide message box" : "\pShow message box");
  219.     SetItem(gEditorMenu, iToolbar, gShowToolbar ? "\pHide toolbar" : "\pShow toolbar");
  220.     EDItem(gEditorMenu, 0, TRUE);
  221.     EDItem(gEditorMenu, iOpenEditor, !editorIsFront);
  222.     EDItem(gEditorMenu, iNewGroup, editorIsFront);
  223.     EDItem(gEditorMenu, iNewBoard, editorIsFront);
  224.     EDItem(gEditorMenu, iEditGroup, editorIsFront && ((groupListActive && groupHighlighted) ||
  225.         (boardListActive && boardHighlighted && !boardLocked)));
  226.     EDItem(gEditorMenu, iDeleteGroup, editorIsFront && ((groupListActive && groupHighlighted) ||
  227.         (boardListActive && boardHighlighted && !boardLocked)));
  228.     EDItem(gEditorMenu, iClearBoard, editorIsFront && boardHighlighted && !boardLocked);
  229.     EDItem(gEditorMenu, iLockBoard, editorIsFront && boardHighlighted);
  230.     EDItem(gEditorMenu, iSaveBoard, editorIsFront && WindowIsModifiedQQ(editorWindow));
  231.     EDItem(gEditorMenu, iMessageBox, editorIsFront && gShowToolbar);
  232.     EDItem(gEditorMenu, iToolbar, editorIsFront);
  233.     
  234. #if USE_WINDOWS_MENU
  235.     EDItem(gWindowsMenu, 0, !gInProgress);
  236.     /* EDItem(gWindowsMenu, iArrange, numOpenWindows>0); */
  237.     EDItem(gWindowsMenu, iZoom, (frontDoc!=0L) && (WindowIsZoomableQQ(frontDoc)));
  238.     EDItem(gWindowsMenu, iSendToBack, numOpenWindows>1);
  239.     AdjustWindowsMenu(frontDoc);
  240. #endif
  241. }
  242.  
  243. void HandleMenu(long mSelect)
  244. {
  245.     short            menuID = HiWord(mSelect);
  246.     short            menuItem = LoWord(mSelect);
  247.     
  248.     switch (menuID)
  249.     {
  250.         case mApple:
  251.             HandleAppleMenu(menuItem);
  252.             break;
  253.         case mFile:
  254.             HandleFileMenu(menuItem);
  255.             break;    
  256.         case mEdit:
  257.             HandleEditMenu(menuItem, FALSE);
  258.             break;
  259.         case mOptions:
  260.             HandleOptionsMenu(menuItem);
  261.             break;
  262.         case mEditor:
  263.             HandleEditorMenu(menuItem);
  264.             break;
  265. #if USE_WINDOWS_MENU
  266.         case mWindows:
  267.             HandleWindowsMenu(menuItem);
  268.             break;
  269. #endif
  270.         default:
  271.             if ((menuID>0) && (menuID<=255))
  272.                 HandleSubMenu(menuID, menuItem);
  273.             break;
  274.     }
  275. }
  276.  
  277. void DoTheCloseThing(WindowPeek theWindow)
  278. /* a standard close procedure, called when "close" is chosen from File menu or when
  279.    a window is closed through its close box */
  280. {
  281.     short            kind;
  282.     
  283.     if (theWindow==0L)
  284.         return;
  285.     
  286.     kind = theWindow ? theWindow->windowKind : 0;
  287.     if (kind<0)        /* DA window or other system window */
  288.         CloseDeskAcc(kind);
  289.     else
  290.     {
  291.         if (WindowHasLayer((WindowRef)theWindow))
  292.         {
  293.             if (OptionKeyWasDown())
  294.             {
  295.                 while ((theWindow=(WindowPeek)GetFrontDocumentWindow())!=0L)
  296.                     CloseTheWindow((WindowRef)theWindow);
  297.             }
  298.             else
  299.             {
  300.                 CloseTheWindow((WindowRef)theWindow);
  301.             }
  302.         }
  303.         else
  304.         {
  305.             DisposeWindow((WindowRef)theWindow);
  306.         }
  307.         
  308.         AdjustMenus();
  309.     }
  310. }
  311.  
  312. static    void HandleAppleMenu(short menuItem)
  313. {
  314.     GrafPtr        savePort;
  315.     Str255        name;
  316.     
  317.     switch (menuItem)
  318.     {
  319.         case iAbout:
  320.             if (!IndWindowExistsQQ(kAboutWindow))
  321.                 OpenTheIndWindow(kAboutWindow, kOpenOldIfPossible);
  322.             else
  323.                 MySelectWindow(GetIndWindowRef(kAboutWindow));
  324.             break;
  325.         case iOtherProducts:
  326.             if (!IndWindowExistsQQ(kOtherProductsWindow))
  327.                 OpenTheIndWindow(kOtherProductsWindow, kOpenOldIfPossible);
  328.             else
  329.                 MySelectWindow(GetIndWindowRef(kOtherProductsWindow));
  330.             break;
  331.         case iHelp:
  332.             if (!IndWindowExistsQQ(kHelpWindow))
  333.                 OpenTheIndWindow(kHelpWindow, kOpenOldIfPossible);
  334.             else
  335.                 MySelectWindow(GetIndWindowRef(kHelpWindow));
  336.             break;
  337.         default:
  338.             if (menuItem > iHelp+1)
  339.             {
  340.                 GetPort(&savePort);
  341.                 GetItem(gAppleMenu, menuItem, name);
  342.                 OpenDeskAcc(name);
  343.                 SetPort(savePort);
  344.             }
  345.             break;
  346.     }
  347. }
  348.  
  349. static    void HandleFileMenu(short menuItem)
  350. {
  351.     WindowRef        theWindow;
  352.     FSSpec            fs;
  353.     TEHandle        hTE;
  354.     
  355.     switch (menuItem)
  356.     {
  357.         case iNew:
  358.             NewGame();
  359.             break;
  360.         case iOpen:
  361.             theWindow=GetIndWindowRef(kMainWindow);
  362.             fs=GetWindowFS(theWindow);
  363.             LoadSaveDispatch(&fs, TRUE, FALSE);
  364.             break;
  365.         case iClose:
  366.             if ((theWindow=GetFrontDocumentWindow())!=0L)
  367.                 DoTheCloseThing((WindowPeek)theWindow);
  368.             else
  369.                 DoTheCloseThing((WindowPeek)FrontWindow());
  370.             break;
  371.         case iSave:
  372.             theWindow=GetIndWindowRef(kMainWindow);
  373.             fs=GetWindowFS(theWindow);
  374.             LoadSaveDispatch(&fs, FALSE, TRUE);
  375.             break;
  376.         case iSaveAs:
  377.             theWindow=GetIndWindowRef(kMainWindow);
  378.             fs=GetWindowFS(theWindow);
  379.             LoadSaveDispatch(&fs, FALSE, FALSE);
  380.             break;
  381. #if USE_PRINTING
  382.         case iPageSetup:
  383.             DeactivateFloatersAndFirstDocumentWindow();
  384.             DoThePageSetup();
  385.             ActivateFloatersAndFirstDocumentWindow();
  386.             break;
  387.         case iPrint:
  388.             theWindow=GetFrontDocumentWindow();
  389.             if ((WindowHasLayer(theWindow)) && (WindowIsPrintableQQ(theWindow)))
  390.             {
  391.                 hTE=GetWindowTE(theWindow);
  392.                 if (hTE!=0L)
  393.                 {
  394.                     DeactivateFloatersAndFirstDocumentWindow();
  395.                     PrintText(hTE);
  396.                     ActivateFloatersAndFirstDocumentWindow();
  397.                 }
  398.             }
  399.             else SysBeep(7);
  400.             break;
  401. #endif
  402.         case iQuit:
  403.             gDone=ShutDownTheProgram();
  404.             break;
  405.     }
  406. }
  407.  
  408. static    void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough)
  409. {
  410.     WindowRef        theWindow;
  411.     DispatchError    resultCode;
  412.     
  413.     if (!alreadyPassedThrough)
  414.         theWindow=FrontWindow();
  415.     else
  416.     {
  417.         theWindow=GetFrontDocumentWindow();
  418.     }
  419.     
  420.     if (theWindow!=0L)
  421.     {
  422.         switch (menuItem)
  423.         {
  424.             case iUndo:            resultCode=UndoDispatch(theWindow);            break;
  425.             case iCut:            resultCode=CutDispatch(theWindow);            break;
  426.             case iCopy:            resultCode=CopyDispatch(theWindow);            break;
  427.             case iPaste:        resultCode=PasteDispatch(theWindow);        break;
  428.             case iClear:        resultCode=ClearDispatch(theWindow);        break;
  429.             case iSelectAll:    resultCode=SelectAllDispatch(theWindow);    break;
  430.             default:            resultCode=kSuccess;                        break;
  431.         }
  432.         
  433.         if ((resultCode==kPassThrough) && (!alreadyPassedThrough))
  434.             HandleEditMenu(menuItem, TRUE);
  435.     }
  436.     else SystemEdit(menuItem-1);
  437. }
  438.  
  439. static    void HandleOptionsMenu(short menuItem)
  440. {
  441.     switch (menuItem)
  442.     {
  443.         case iPlace:
  444.             PlaceUnknownPiece(GetIndWindowRef(kMainWindow));
  445.             break;
  446.         case iRemove:
  447.             RemovePiece(GetIndWindowRef(kMainWindow));
  448.             break;
  449.         case iSound:
  450.             gSoundToggle=!gSoundToggle;
  451.             DoSound(sound_on, TRUE);
  452.             break;
  453.         case iMusic:
  454.             gMusicToggle=!gMusicToggle;
  455.             if (gMusicToggle)
  456.                 StartTheMusic();
  457.             else
  458.                 CloseTheMusicChannel();
  459.             break;
  460.         case iMessages:
  461.             gDisplayMessages=!gDisplayMessages;
  462.             break;
  463.         case iEffects:
  464.             gGraphicEffects=!gGraphicEffects;
  465.             break;
  466.         case iAskBeforeClosing:
  467.             gAskBeforeClosing=!gAskBeforeClosing;
  468.             break;
  469.         case iShowFloat:
  470.             ToggleShowFloatingWindow();
  471.             break;
  472.         case iAnchorFloat:
  473.             ToggleAnchorFloatingWindow();
  474.             break;
  475.         case iAutoSize:
  476.             gAutoSize=!gAutoSize;
  477.             break;
  478.         case iPieceSize:
  479.             DoPieceSizeDialog();
  480.             DealWithChangedPieceSize(GetIndWindowRef(kMainWindow));
  481.             break;
  482.     }
  483. }
  484.  
  485. static    void HandleEditorMenu(short menuItem)
  486. {
  487.     WindowRef        theWindow;
  488.     
  489.     theWindow=GetIndWindowRef(kEditorWindow);
  490.     if (menuItem==iOpenEditor)
  491.     {
  492.         if (theWindow==0L)
  493.             OpenTheIndWindow(kEditorWindow, kAlwaysOpenNew);
  494.         else
  495.             MySelectWindow(theWindow);
  496.         return;
  497.     }
  498.     else if (theWindow==0L)
  499.         return;
  500.     
  501.     RememberEditorButtonState(theWindow);
  502.     
  503.     switch (menuItem)
  504.     {
  505.         case iNewGroup:
  506.             DoNewDispatch(theWindow, TRUE);
  507.             break;
  508.         case iNewBoard:
  509.             DoNewDispatch(theWindow